package com.echo.http.imageload;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.echo.SportsApplication;
import com.echo.http.VolleyRequestManager;

/**
 * Created by EchoGe on 2016/4/23.
 */
public class ImageLoadManager {
    private static String TAG=ImageLoadManager.class.getSimpleName();

    //图片缓存对象
    private static ImageLoader.ImageCache mImageCache=new ImageCacheUtil();
    //获取ImageLoader对象
    public static ImageLoader mImageLoader=new ImageLoader(
            VolleyRequestManager.mRequestQueue,mImageCache);

    public static Bitmap mBitmap;
    private static final int FADE_IN_TIME = 400;

    /**
     * 获取ImageListener
     */
    public static ImageLoader.ImageListener getImageListener(final ImageView view,
                                                             final Bitmap defaultImage, final Bitmap errorImage) {

        return new ImageLoader.ImageListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // 回调失败
                if (errorImage != null) {
                    view.setImageBitmap(errorImage);
                }
            }

            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                // 回调成功
                if(view != null){
                    if (response.getBitmap() != null) {
                        mBitmap = response.getBitmap();
                        setImageBitmapWithAlpha(view, mBitmap, defaultImage);
                        // view.setImageBitmap(mBitmap);
                    } else if (defaultImage != null) {
                        view.setImageBitmap(defaultImage);
                    }
                } else {
                    if (response.getBitmap() != null) {
                        mBitmap = response.getBitmap();
                    }
                }
            }
        };

    }


    public static Bitmap loadImage(String url, ImageView view,
                                   Bitmap defaultImage, Bitmap errorImage) {
        mImageLoader.get(url, ImageLoadManager.getImageListener(view,
                defaultImage, errorImage), 0, 0);
        return mBitmap;
    }


    private static void setImageBitmapWithAlpha(final ImageView imageView,
                                                Bitmap bitmap, Bitmap defaultImage) {
        final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
                new ColorDrawable(android.R.color.transparent),
                new BitmapDrawable(SportsApplication.getInstance().getResources(),
                        bitmap) });
        // Set background to loading bitmap
        if (defaultImage != null) {
            imageView.setImageDrawable(new BitmapDrawable(SportsApplication
                    .getInstance().getResources(), defaultImage));
        }
        imageView.setImageDrawable(td);
        td.startTransition(FADE_IN_TIME);
    }

}
